package xcmg.device.service.company;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xcmg.device.dao.entity.company.DealerClassDO;
import xcmg.device.dao.mapper.DealerClassMapper;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.vo.company.DealerClassVO;
import yb.ecp.fast.infra.util.ListUtil;
import yb.ecp.fast.infra.util.Ref;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by sqq on 2017/12/21.
 */
@Service
public class DealerClassService {

    @Autowired
    private DealerClassMapper dealerClassMapper;

    @Autowired
    private CompanyDealerService companyDealerService;

    public ErrorCode insert(DealerClassVO dealerClassVO){
        dealerClassVO.setCreateDate(new Date());
        dealerClassVO.setUpdateDate(new Date());
        dealerClassVO.setIsDel(false);
        dealerClassVO.setClassValue(getClassValue(dealerClassVO.getpId(),dealerClassVO.getOrgId()));
        DealerClassDO dealerClassDO=getDO(dealerClassVO);
        Integer nameCount=dealerClassMapper.nameCount(dealerClassDO);
        if(nameCount>0){
            return ErrorCode.ClassNameDuplicated;
        }
        ErrorCode ret= dealerClassMapper.insert(dealerClassDO)>0? ErrorCode.Success:ErrorCode.Failure;
        dealerClassVO.setId(dealerClassDO.getId());
        return ret;
    }

    public ErrorCode update(DealerClassVO dealerClassVO){
        DealerClassVO newVO=new DealerClassVO();
        newVO.setId(dealerClassVO.getId());
        newVO.setClassCode(dealerClassVO.getClassCode());
        newVO.setClassName(dealerClassVO.getClassName());
        newVO.setUpdateId(dealerClassVO.getUpdateId());
        newVO.setUpdateDate(new Date());
        DealerClassDO dealerClassDO=getDO(newVO);
        DealerClassDO sourceDO=dealerClassMapper.item(dealerClassDO.getId());
        if(sourceDO==null)
        {
            return ErrorCode.IllegalArument;
        }
        sourceDO.setClassName(dealerClassDO.getClassName());
        Integer nameCount=dealerClassMapper.nameCount(sourceDO);
        if(nameCount>0){
            return ErrorCode.ClassNameDuplicated;
        }
        return dealerClassMapper.update(dealerClassDO)>0?ErrorCode.Success:ErrorCode.Failure;
}

    public List<DealerClassVO> list(DealerClassVO dealerClassVO){
        dealerClassVO.setIsDel(false);
        List<DealerClassDO> listDO=dealerClassMapper.list(dealerClassVO);
        List<DealerClassVO> voList = new ArrayList<>();
        if(listDO==null){
            return  voList;
        }
        for (DealerClassDO dealerClassDO:listDO
             ) {
            voList.add(getVO(dealerClassDO));
        }
        return voList;
    }

    public ErrorCode delete(DealerClassVO dealerClassVO, Ref<List<String>> classNameList)
    {
        DealerClassDO dealerClassDO = dealerClassMapper.item(dealerClassVO.getId());
        if (dealerClassDO == null)
        {
            return ErrorCode.IllegalArument;
        }
        List<String> className = new ArrayList<>();

        List<DealerClassDO> classDOList = dealerClassMapper.listIdByValue(dealerClassDO);

        if (ListUtil.isNullOrEmpty(classDOList))
        {
            return ErrorCode.IllegalArument;
        }

        for (DealerClassDO classDO : classDOList)
        {
            int companyCount = companyDealerService.countCompanyByType(classDO.getId());
            if (companyCount > 0)
            {
                className.add(classDO.getClassName());
            }
        }

        if (!ListUtil.isNullOrEmpty(className))
        {
            classNameList.set(className);
            return ErrorCode.ClassIsUsed;
        }

        return dealerClassMapper.deleteByValue(dealerClassDO.getOrgId(), dealerClassDO.getClassValue()) > 0 ? ErrorCode.Success : ErrorCode.Failure;
    }

    private DealerClassDO getDO(DealerClassVO dealerClassVO){
        if(dealerClassVO==null){return null;}
        DealerClassDO dealerClassDO=new DealerClassDO();
        dealerClassDO.setId(dealerClassVO.getId());
        dealerClassDO.setOrgId(dealerClassVO.getOrgId());
        dealerClassDO.setpId(dealerClassVO.getpId());
        dealerClassDO.setClassName(dealerClassVO.getClassName());
        dealerClassDO.setClassCode(dealerClassVO.getClassCode());
        dealerClassDO.setClassValue(dealerClassVO.getClassValue());
        dealerClassDO.setIsDel(dealerClassVO.getIsDel());
        dealerClassDO.setCreateDate(dealerClassVO.getCreateDate());
        dealerClassDO.setCreateId(dealerClassVO.getCreateId());
        dealerClassDO.setUpdateDate(dealerClassVO.getUpdateDate());
        dealerClassDO.setUpdateId(dealerClassVO.getUpdateId());
        return dealerClassDO;
    }
    private DealerClassVO getVO(DealerClassDO dealerClassDO){
        if(dealerClassDO==null){return null;}
        DealerClassVO dealerClassVO=new DealerClassVO();
        dealerClassVO.setId(dealerClassDO.getId());
        dealerClassVO.setOrgId(dealerClassDO.getOrgId());
        dealerClassVO.setpId(dealerClassDO.getpId());
        dealerClassVO.setClassName(dealerClassDO.getClassName());
        dealerClassVO.setClassCode(dealerClassDO.getClassCode());
        dealerClassVO.setClassValue(dealerClassDO.getClassValue());
        dealerClassVO.setIsDel(dealerClassDO.getIsDel());
        dealerClassVO.setCreateDate(dealerClassDO.getCreateDate());
        dealerClassVO.setCreateId(dealerClassDO.getCreateId());
        dealerClassVO.setUpdateDate(dealerClassDO.getUpdateDate());
        dealerClassVO.setUpdateId(dealerClassDO.getUpdateId());
        dealerClassVO.setChildCount(dealerClassDO.getChildCount());
        return dealerClassVO;
    }

    private String getClassValue(Long pId,String orgId){
        String classValue=dealerClassMapper.getMaxValue(pId,orgId);
        if(classValue==null||classValue==""){
            if(pId==0){
                classValue="001";
            }else {
                DealerClassDO dealerClassDO=dealerClassMapper.item(pId);
                classValue=dealerClassDO.getClassValue()+"001";
            }
        }else {
            String endValue= classValue.substring(classValue.length()-3,classValue.length());
            String startValue= classValue.substring(0,classValue.length()-3);
            Integer intValue= Integer.parseInt(endValue)+1;
            if(intValue<10){
                classValue=startValue+"00"+String.valueOf(intValue);
            }if(intValue>=10 && intValue<100){
                classValue=startValue+"0"+String.valueOf(intValue);
            }if(intValue>=100){
                classValue=startValue+String.valueOf(intValue);
            }
        }
        return classValue;
    }
}
